﻿
namespace System
{
    public static class DateTimeExt
    {
        public static TimeZoneInfo EasternTimeZone
        {
            get
            {
                return TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            }
        }

        public static TimeZoneInfo PacificTimeZone
        {
            get
            {
                return TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            }
        }

        public static int EstToUnix(this DateTime val)
        {
            var date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            date = TimeZoneInfo.ConvertTimeFromUtc(date, EasternTimeZone);
            return (int)(val - date).TotalSeconds;
        }

        public static DateTime UnixToEst(long seconds)
        {
            var date = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            date = date.AddSeconds(seconds);
            date = TimeZoneInfo.ConvertTimeFromUtc(date, EasternTimeZone);
            return date;
        }

        public static string ToDateString(this DateTime date)
        {
            return date.ToString("MMMM dd, yyyy");
        }

        public static string ToDateTimeString(this DateTime date)
        {
            return date.ToString("MMMM dd, yyyy hh:mm:ss PST");
        }

        public static DateTime? ToDateTime(this string s)
        {
            var date = new DateTime();
            if (!DateTime.TryParseExact(s, new string[] { "MMMM dd, yyyy", "MMM dd, yyyy" }, System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat, System.Globalization.DateTimeStyles.None, out date))
                return null;
            return date;
        }

    }
}
